import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class MemorandumSelectAllPageViewModel extends BaseViewModel {
  PublishSubject memorandumLoadingState = PublishSubject<CommonLoadingState?>();
  UserMemorandumListResponse? _userMemorandumResponse;

  UserMemorandumListResponse? get userMemorandumResp => _userMemorandumResponse;

  List<String?> deleteList = [];

  // 跳转到编辑备忘录页面
  Future<void> turnToEditMemorandumPage(
    BuildContext context, {
    String? noteId,
    bool? isEditMode,
  }) async {
    final params = <String, Object?>{};
    if (noteId?.isNotEmpty == true) {
      params['note_id'] = noteId;
    }
    switch (isEditMode) {
      case true:
        params['is_edit_mode'] = '1';
        break;
      default:
        params['is_edit_mode'] = '0';
        break;
    }
    return await getApplication().routerNavigateTo(
      context,
      RoutersPath.editMemorandumPage,
      params: params,
    );
  }

  // 返回上一页
  Future<void> backPage(BuildContext context) async {
    await getApplication().routerPop(context);
  }

  // 拉取备忘录信息
  Future<void> pullUserMemorandumList({
    int pageSize = 10,
    int currentPage = 1,
    bool refreshQuickly = false,
    bool append = false,
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    if (_userMemorandumResponse?.haveMore == false && !refreshQuickly) {
      return;
    }
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    params['page_size'] = pageSize;
    if (append) {
      params['current_page'] = (_userMemorandumResponse?.currentPage ?? 0) + 1;
    } else {
      params['current_page'] = currentPage;
    }
    final result = await getNetworkUtil()
        .postHttp(UrlContract.pullUserMemorandumList, params: params);
    final resp = UserMemorandumListResponse.fromMap(result);
    if (resp.status == 0) {
      if (append) {
        _userMemorandumResponse?.currentPage = resp.currentPage;
        _userMemorandumResponse?.haveMore = resp.haveMore;
        _userMemorandumResponse?.pageSize = resp.pageSize;
        resp.memorandumList?.forEach((element) {
          _userMemorandumResponse?.memorandumList?.add(element);
        });
      } else {
        _userMemorandumResponse = resp;
      }
      memorandumLoadingState.add(CommonLoadingState.successTypeOne);
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      memorandumLoadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  Future<void> deleteAllMemorandumFromList({
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    params['note_id_list'] = deleteList;
    final result = await getNetworkUtil()
        .postHttp(UrlContract.deleteUserMemorandumList, params: params);
    final resp = CommonResponse.fromMap(result);
    if (resp.status == 0) {
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  // 获取备忘录上次修改时间
  String getLastEditTime({String? time}) {
    int? timeInt = int.tryParse(time ?? '');
    DateTime times = DateTime.fromMillisecondsSinceEpoch(timeInt ?? 0);
    final dateTime =
        DateTime.fromMillisecondsSinceEpoch(times.millisecondsSinceEpoch);
    if (dateTime.minute < 10) {
      return '${dateTime.year}年${dateTime.month}月${dateTime.day}日 ${dateTime.hour}:0${dateTime.minute}';
    }
    return '${dateTime.year}年${dateTime.month}月${dateTime.day}日 ${dateTime.hour}:${dateTime.minute}';
  }

  void changeAllSelect() {
    _userMemorandumResponse?.memorandumList?.forEach(
      (element) {
        element?.isSelect = true;
        deleteList.add(element?.noteId);
      },
    );
  }

  void changeAllUnSelect() {
    deleteList.clear();
    _userMemorandumResponse?.memorandumList?.forEach(
      (element) {
        element?.isSelect = false;
      },
    );
  }

  bool checkSelectAll() {
    int selectCount = 0;
    _userMemorandumResponse?.memorandumList?.forEach((element) {
      if (element?.isSelect == true) {
        selectCount++;
      }
    });
    if (selectCount == _userMemorandumResponse?.memorandumList?.length) {
      return true;
    }
    return false;
  }
}
